package club.ruanx.movie.init;

import club.ruanx.core.init.AbstractSystemInitializer;
import club.ruanx.movie.entity.Movie;
import club.ruanx.movie.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 阮胜
 * @date 2018/11/10 16:12
 */
@Component
public class MovieInfoInitializer extends AbstractSystemInitializer {
    @Value("${movie.data.init:true}")
    private boolean isInit;
    private final MovieRepository movieRepository;

    public MovieInfoInitializer(MovieRepository movieRepository) {
        this.movieRepository = movieRepository;
    }

    @Override
    protected void invoke() throws Exception {
        Movie.MovieBuilder movieBuilder = Movie.builder().name("测试电影").actors("徐峥,梁朝伟").directors("张艺谋,李安")
                .areas("[1,2,3]").duration("1小时").cover("http://file.rs.cn/GROUP00/M00/00/00/wKgBuVvmyp-Afgm9AAFgUwtzCWs796.jpg")
                .initialReleaseDate(new Date()).introduction("描述").languages("[1,2,3]").rate(9.8f)
                .types("[1,2,3]");
        for (int i = 0; i < 10; i++) {
            movieRepository.save(movieBuilder.build());
        }

    }

    @Override
    public int obtainInitIndex() {
        return 1;
    }

    @Override
    public boolean preInit() throws Exception {
        return isInit && movieRepository.count() == 0;
    }
}
